##package rightheader

window.rightheader = {}

( ->

##private

  $user_menu = null
  $user_menu_content = null
  $language_menu = null
  $language_menu_content = null
  $user_menu_timer = null
  $language_menu_timer = null

  # Menu stuff

  un_menu_open = ->
    li_menu_close()
    $user_menu_content = $user_menu.find('ul')
    $user_menu_content.fadeIn(100)

  un_menu_close = ->
    $user_menu_content.fadeOut(100) if $user_menu_content

  un_menu_timer = ->
    $user_menu_timer = setTimeout(un_menu_close, 500)

  un_menu_canceltimer = ->
    if $user_menu_timer
      clearTimeout($user_menu_timer)
      $user_menu_timer = null

  li_menu_open = ->
    un_menu_close()
    $language_menu_content = $language_menu.find('ul')
    $language_menu_content.fadeIn(100)

  li_menu_close = ->
    $language_menu_content.fadeOut(100) if $language_menu_content

  li_menu_timer = ->
    $language_menu_timer = setTimeout(li_menu_close, 500)

  li_menu_canceltimer = ->
    if $language_menu_timer
      clearTimeout($language_menu_timer)
      $language_menu_timer = null

  # Other stuff

  class_changed = ->
    unless this.innerHTML == login.class
      # Use overlay because it's taking a while
      $('#to').fadeIn()
      #TODO: Put into variable
      $('#li .class-rb').removeClass 'active'
      $(this).addClass 'active'
      $('#cl').html this.innerHTML
      login.class = this.innerHTML
      login.get_vocab ->
        $('#to').fadeOut()
      #TODO: Get everything else when grammar etc is added

##public

  rightheader.view_settings = ->
    unless $('#settingsoverlay').is ':visible'
      $('#settingsoverlay').fadeIn()
    else
      $('#settingsoverlay').fadeOut()

##initialize

  rightheader.initialize = ->
    $user_menu = $('#un')
    $language_menu = $('#li')
    $user_menu.mouseenter un_menu_open
    $user_menu.hover un_menu_canceltimer, un_menu_timer
    $language_menu.mouseenter li_menu_open
    $language_menu.hover li_menu_canceltimer, li_menu_timer
    $('#li .class-rb').click class_changed
    console.log($('a#settingsbtn'))
    $('a#settingsbtn').click -> console.log "Viewing settings"

##end

)()